package com.lottery.im.widget.pullextend;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import com.lottery.im.R;
import com.lottery.im.util.DisplayHelper;

/**
 * 这个类封装了下拉刷新的布局
 */
public class ExtendListHeader extends ExtendLayout {

  float containerHeight = DisplayHelper.dpToPx(60);
  float listHeight =
      (int) getResources().getDimensionPixelSize(R.dimen.extend_header_height);

  boolean arrivedListHeight = false;
  private RecyclerView mRecyclerView;

  /**
   * 原点
   */

  private ExpendPoint mExpendPoint;

  /**
   * 构造方法
   *
   * @param context context
   */
  public ExtendListHeader(Context context) {
    super(context);
  }

  /**
   * 构造方法
   *
   * @param context context
   * @param attrs attrs
   */
  public ExtendListHeader(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected void bindView(View container) {
    mRecyclerView = findViewById(R.id.list);
    mExpendPoint = findViewById(R.id.expend_point);
  }

  public RecyclerView getRecyclerView() {
    return mRecyclerView;
  }

  @Override
  protected View createLoadingView(Context context, AttributeSet attrs) {
    return LayoutInflater.from(context).inflate(R.layout.extend_header, null);
  }

  @Override
  public int getContentSize() {
    return (int) (containerHeight);
  }

  @Override
  public int getListSize() {
    return (int) (listHeight);
  }

  @Override
  protected void onReset() {
    mExpendPoint.setVisibility(VISIBLE);
    mExpendPoint.setAlpha(1);
    mExpendPoint.setTranslationY(0);
    mRecyclerView.setTranslationY(0);
    arrivedListHeight = false;
  }

  @Override
  protected void onReleaseToRefresh() {
  }

  @Override
  protected void onPullToRefresh() {

  }

  @Override
  protected void onArrivedListHeight() {
    arrivedListHeight = true;
  }

  @Override
  protected void onRefreshing() {
  }

  @Override
  public void onPull(int offset) {
    if (!arrivedListHeight) {
      mExpendPoint.setVisibility(VISIBLE);
      float percent = Math.abs(offset) / containerHeight;
      int moreOffset = Math.abs(offset) - (int) containerHeight;
      if (percent <= 1.0f) {
        mExpendPoint.setPercent(percent);
        mExpendPoint.setTranslationY(-Math.abs(offset) / 2 + mExpendPoint.getHeight() / 2);
        mRecyclerView.setTranslationY(-containerHeight);
      } else {
        float subPercent = (moreOffset) / (listHeight - containerHeight);
        subPercent = Math.min(1.0f, subPercent);
        mExpendPoint.setTranslationY(-(int) containerHeight / 2 + mExpendPoint.getHeight() / 2
            + (int) containerHeight * subPercent / 2);
        mExpendPoint.setPercent(1.0f);
        float alpha = (1 - subPercent * 2);
        mExpendPoint.setAlpha(Math.max(alpha, 0));
        mRecyclerView.setTranslationY(-(1 - subPercent) * containerHeight);
      }
    }
    if (Math.abs(offset) >= listHeight) {
      mExpendPoint.setVisibility(INVISIBLE);
      // 头部全部显示以后 继续下拉 覆盖头部
      //mRecyclerView.setTranslationY(-(Math.abs(offset) - listHeight) / 2);
      // 头部全部显示以后 继续下拉 头部跟随下移
      mRecyclerView.setTranslationY(0);
    }
  }
}
